package com.wanmao.test;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.wanmao.test.bean.GZJLBean;
import com.wanmao.test.service.GZJLService;
import com.wanmao.test.view.XListView;

import java.util.ArrayList;
import java.util.List;



public class FaultRecordActiviyt extends BaseActivity {
    private XListView lv_device;
    private MyAdapter adapter;
    private String RWID,ZYLX;
    private GZJLService gzjlService = new GZJLService(this);
    private List<GZJLBean> gzjlBeanList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fault_record);
        Intent intent =getIntent();
        RWID=intent.getStringExtra("RWID");
        ZYLX=intent.getStringExtra("ZYLX");
        initData();
        intenView();
    }

    private void initData() {
        gzjlBeanList = new ArrayList<>();
        gzjlBeanList = gzjlService.findByRWID(RWID);
    }
    private void intenView() {
        adapter= new MyAdapter(FaultRecordActiviyt.this,gzjlBeanList);
        lv_device=(XListView) findViewById(R.id.lv_fault_recird);
        lv_device.setAdapter(adapter);
        lv_device.setPullLoadEnable(false);
        lv_device.setPullRefreshEnable(false);


    }
    class MyAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private Context mContext;
        private List<GZJLBean> mDatas;

        public MyAdapter(Context context, List<GZJLBean> mDatas) {
            mInflater = LayoutInflater.from(context);
            this.mContext = context;
            this.mDatas = mDatas;
        }

        @Override
        public int getCount() {
            return mDatas.size();
        }

        @Override
        public Object getItem(int position) {
            return mDatas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_fault_record, null);
                viewHolder = new ViewHolder();
                viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_item_fault_title);
                viewHolder.tv_GZID = (TextView) convertView.findViewById(R.id.tv_GZID);
                viewHolder.tv_GZLX = (TextView) convertView.findViewById(R.id.tv_GZLX);
                viewHolder.tv_GZFSSJ = (TextView) convertView.findViewById(R.id.tv_GZFSSJ);
                viewHolder.btn_do = (Button) convertView.findViewById(R.id.btn_item_fault_record);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            String title = mDatas.get(position).getBDZMC();
            String GZID = mDatas.get(position).getOBJ_ID().substring(0,36);
            String GZLX = mDatas.get(position).getGZLX();
            String GZFSSJ = mDatas.get(position).getGZFSSJ();
            viewHolder.tv_title.setText(title);
            viewHolder.tv_GZID.setText("故障ID:"+GZID);
            viewHolder.tv_GZLX.setText(GZLX);
            viewHolder.tv_GZFSSJ.setText(GZFSSJ);
            final int m =position;
            viewHolder.btn_do.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent();
                    intent.setClass(FaultRecordActiviyt.this, FaultDetailsActivity.class);
                    intent.putExtra("OBJ_ID", mDatas.get(m).getOBJ_ID());
                    intent.putExtra("ZYLX", ZYLX);
                    startActivity(intent);
                }
            });
            return convertView;
        }
    }


    public static class ViewHolder {
        public TextView tv_title,tv_GZID,tv_GZLX,tv_GZFSSJ;
        public Button btn_do;
    }
}
